package com.hgm.study.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.google.accompanist.insets.LocalWindowInsets
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.hgm.study.ui.theme.Blue200
import com.hgm.study.ui.theme.Blue700

/**
 * 统一标题栏
 */

//标题栏高度
val appBarHeight = 56.dp

@Composable
fun TopAppBar(modifier: Modifier = Modifier, content: @Composable () -> Unit) {
      //转换状态栏高度为：Dp
      val statusBarHeightDp = with(LocalDensity.current) {
            LocalWindowInsets.current.statusBars.top.toDp()
      }
      //设置透明状态栏
      var systemUiController = rememberSystemUiController()
      LaunchedEffect(key1 = Unit) {
            systemUiController.setStatusBarColor(Color.Transparent, darkIcons = true)
      }

      Row(
            modifier = Modifier
                  .fillMaxWidth()
                  .background(
                        Brush.linearGradient(
                              listOf(Blue700, Blue200)
                        )
                  )
                  .height(appBarHeight + statusBarHeightDp)
                  .padding(top = statusBarHeightDp)
                  .then(modifier),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
      ) {
            content()
      }
}

@Preview
@Composable
fun TopAppBarPreview() {
      TopAppBar() {
            Text("123")
      }
}

